__author__ = "Rekt77"

import sys
import glob
import os
from PIL import Image
import shutil
import math
import numpy as np

BLACK = 0 
WHITE = 255

#def B_sequenceTopixel():

allFile = glob.glob("*")
ExtensionFile = glob.glob("*.*")
TargetFile = [file for file in allFile if file not in ExtensionFile]


for fname in TargetFile:
  Pixel_List = list()
  try:
    with open(fname, 'rb') as f_src:
      print("[+] PE32 -> GrayScale image : " + fname)
      size = math.ceil(math.sqrt(os.path.getsize(fname)))
      print("[+] Img Size %d*%d"%(size,size))
      while True:
        s = f_src.read(1)
        if len(s) == 0:
          break  
        s = int(ord(s))
        Pixel_List.append(s)
        #Pixel_List= Pixel_List+[0 for i in range(0,(size*size - len(Pixel_List)-1))]

      print(len(Pixel_List))
      grayScaleImage = Image.new("L",(size,size),"black")
      #BlackImageList = np.array(BlackImageList)
      #BlackImageList.reshape(size,size)
      grayScaleImage.putdata(Pixel_List)
      grayScaleImage.save(fname+".png")
      
  except IOError :
      print >> sys.stderr, 'Can\'t find file.'

